package com.source.hotelbath.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.source.hotelbath.R;
import com.source.hotelbath.model.AllGoodsListBean;
import com.source.hotelbath.utils.ImageUtils;
import com.source.hotelbath.utils.TintDrawable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhouMeng on 2017/9/25.
 */

public class TitleExpandAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<AllGoodsListBean> allGoodsList=new ArrayList<>();
    private Drawable drawable;
    private Bitmap img_a;

    public TitleExpandAdapter(Context context, List<AllGoodsListBean> allGoodsList) {
        this.context=context;
        this.allGoodsList=allGoodsList;
        drawable = TintDrawable.tintGetDrawable(context, R.drawable.down_more, R.color.dark_gray_737272);
        rotateAnimation();
    }

    public Bitmap rotateAnimation(){
        Bitmap img = TintDrawable.drawableToBitmap(drawable);


        Matrix matrix = new Matrix();
        matrix.postRotate(180); /*翻转180度*/
        int width = img.getWidth();
        int height = img.getHeight();
        img_a = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
       boolean save= ImageUtils.saveBitmap("saveBitmap",img_a);
        Log.d("save",save+"");
        return img_a;


    }

    @Override
    public int getGroupCount() {
        return allGoodsList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return allGoodsList.get(groupPosition).getGoodsList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return getGroup(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return getChild(groupPosition,childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.grounp_item, null);
            holder = new ViewHolder();
            holder.tv_grounp_name = (TextView) convertView
                    .findViewById(R.id.tv_grounp_name);
            holder.iv_all= (ImageView) convertView.findViewById(R.id.iv_all);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_grounp_name.setText(allGoodsList.get(groupPosition).getCatName());
        if (isExpanded){
            holder.iv_all.setImageResource(R.drawable.up);
//            holder.iv_all.setImageBitmap(img_a);
        }else{
            holder.iv_all.setImageResource(R.drawable.down);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewChildHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.chind_item, null);
            holder = new ViewChildHolder();
            holder.tv_name = (TextView) convertView
                    .findViewById(R.id.tv_name);
            holder.tv_price = (TextView) convertView
                    .findViewById(R.id.tv_price);
            holder.iv_select= (ImageView) convertView.findViewById(R.id.iv_select);
            convertView.setTag(holder);
        } else {
            holder = (ViewChildHolder) convertView.getTag();
        }
        holder.tv_name.setText(allGoodsList.get(groupPosition).getGoodsList().get(childPosition).getGoodsName());
        holder.tv_price.setText(allGoodsList.get(groupPosition).getGoodsList().get(childPosition).getWashPrice());
        if (allGoodsList.get(groupPosition).getGoodsList().get(childPosition).getCheckType()==0){
            holder.iv_select.setImageResource(R.drawable.un_select);
        }else {
            holder.iv_select.setImageResource(R.drawable.select);
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class ViewHolder {
        TextView tv_grounp_name;
        ImageView iv_all;
    }
    class ViewChildHolder{
        ImageView iv_select;
        TextView tv_name;
        TextView tv_price;
    }
}
